package com.example.workmode.AppManager;

import java.util.List;
import android.app.ActivityManager;
import android.app.admin.DevicePolicyManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.widget.Toast;
 
public class CheckRunningApplicationReceiver extends BroadcastReceiver {
 
	private static final int ADMIN_INTENT = 15;
    private static final String description = "Sample Administrator description";
	private DevicePolicyManager mDevicePolicyManager; 
    private ComponentName mComponentName;  
    private String blockingApps;
    private SharedPreferences pref;
    
    public final String TAG = "CRAR"; // CheckRunningApplicationReceiver
 
    @Override
    public void onReceive(Context aContext, Intent anIntent) {
         
        try {
             
            // Using ACTIVITY_SERVICE with getSystemService(String) 
            // to retrieve a ActivityManager for interacting with the global system state.
             
            ActivityManager am = (ActivityManager) aContext
                    .getSystemService(Context.ACTIVITY_SERVICE);
             
            // Return a list of the tasks that are currently running, 
            // with the most recent being first and older ones after in order.
            // Taken 1 inside getRunningTasks method means want to take only 
            // top activity from stack and forgot the olders.
             
            List<ActivityManager.RunningTaskInfo> alltasks = am
                    .getRunningTasks(1);
 
            // 
            for (ActivityManager.RunningTaskInfo aTask : alltasks) {
            	pref = aContext.getSharedPreferences("MyPrefs", 0);
            	blockingApps = pref.getString("blockingApps", null);
                //String packageName = "com.example.pairsgame.PairsGame";
                 
            	String packageName = aTask.topActivity.getPackageName();
                if (blockingApps.contains(packageName))
                {
                   // When opens this example screen then show a alert message
                   //Toast.makeText(aContext, "Check Current Running Application Example Screen.", 
                   //                Toast.LENGTH_LONG).show();   
                   
                   Intent startMain = new Intent(Intent.ACTION_MAIN);
                   startMain.addCategory(Intent.CATEGORY_HOME);
                   startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                   aContext.startActivity(startMain);
                   am.killBackgroundProcesses(packageName);
                  
                }
                 
                 
                // These are showing current running activity in logcat with 
                // the use of different methods
                /* 
                Log.i(TAG, "===============================");
                 
                    Log.i(TAG, "aTask.baseActivity: "
                                + aTask.baseActivity.flattenToShortString());
                     
                    Log.i(TAG, "aTask.baseActivity: "
                                + aTask.baseActivity.getClassName());
                     
                    Log.i(TAG, "aTask.topActivity: "
                                + aTask.topActivity.flattenToShortString());
                     
                    Log.i(TAG, "aTask.topActivity: "
                                + aTask.topActivity.getClassName());
                 
                Log.i(TAG, "===============================");
                 
                 */
            }
 
        } catch (Throwable t) {
            Log.i(TAG, "Throwable caught: "
                        + t.getMessage(), t);
        }
         
    }
 
}